<template>
  <div class="comment-list">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <!-- <van-cell v-for="item in list" :key="item" :title="item" /> -->
      <commentItem
        v-for="(item, index) in list"
        :key="index"
        :comment="item"
        @reply-click="$emit('reply-click', $event)"
      />
    </van-list>
  </div>
</template>

<script>
import { getComments } from "@/api/comment";
import commentItem from "@/views/article/components/comment-item";
export default {
  name: "CommentList",
  data() {
    return {
      // list: [],
      loading: false,
      finished: false,
      offset: null,
      limit: 10,
    };
  },
  components: {
    commentItem,
  },
  props: {
    source: {
      type: [Number, String, Object],
      required: true,
    },
    list: {
      type: Array,
      default: () => [],
    },
    type: {
      type: String,
      validator(value) {
        return ["a", "c"].includes(value);
      },
      default: "a",
    },
  },
  created() {
    this.loading = true;
    this.onLoad();
  },
  methods: {
    async onLoad() {
      // 异步更新数据
      try {
        const { data } = await getComments({
          type: "a",
          source: this.source.toString(),
          limit: this.limit,
          offset: this.offset,
        });
        console.log(data);
        this.$emit("onload-success", data.data);
        // console.log(this.list);
        const { results } = data.data;
        this.list.push(...results);
        this.loading = false;
        if (results.length) {
          // 有,获取下一页的数据页码
          this.offset = data.data.last_id;
        } else {
          // 没有, finished
          this.finished = true;
        }
      } catch (error) {
        this.$toast("评论加载失败!");
        console.log(error);
      }
    },
  },
};
</script>

<style lang="less" scoped>
</style>
